//
//  DrawWinVC.m
//  Cat
//
//  Created by dllo on 15/11/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
// http://api.huapipi.com/painting/top/normal?days=3&deviceId=1826162&pnum=1&uid=1033268

// http://api.huapipi.com/show/v2/latest/10?deviceId=1826162&uid=1033268&w=720&h=1280 总榜
// http://api.huapipi.com/painting/top/newcomer?days=3&deviceId=1826162&pnum=1&uid=1033268 新人榜



#import "DrawWinVC.h"
#import "CYHDrawNewVC.h"
#import "CYHDrawNewCell.h"
#import "CYHHotOtherVC.h"

@interface DrawWinVC ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UITableView *WinTableView;
@property (nonatomic, strong) NSMutableArray *WinArr;

@property (nonatomic, strong) UICollectionView *NewCollectionView;
@property (nonatomic, strong) NSMutableArray *fourArr;
@property (nonatomic, assign) NSInteger pnum;
@property (nonatomic, copy) NSString *verStr;

@property (nonatomic, copy) NSString *URL;
@property (nonatomic, assign) BOOL isSelete;
@end

@implementation DrawWinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    
    //
    [self createData];
}

#pragma mark - createView
- (void)createView {
    // tableView
    self.WinTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT + 40) style:UITableViewStylePlain];
    [self.view addSubview:self.WinTableView];
    self.WinTableView.separatorStyle = NO;
    self.WinTableView.delegate = self;
    self.WinTableView.dataSource = self;
    
    [self.WinTableView registerClass:[HotCell class] forCellReuseIdentifier:@"Win"];
    
    // tableView footView - collection
    UICollectionViewFlowLayout *flowyou = [[UICollectionViewFlowLayout alloc] init];
    //    flowyou.MyDelegate = self;
    flowyou.itemSize = CGSizeMake(VIEW_WIDTH / 2 - 10, VIEW_HEIGHT / 3);
    flowyou.minimumInteritemSpacing = 5;
    flowyou.minimumLineSpacing = 10;
    flowyou.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.NewCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowyou];
    self.NewCollectionView.delegate = self;
    self.NewCollectionView.dataSource = self;
    [self.view addSubview:self.NewCollectionView];
    self.NewCollectionView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    self.WinTableView.tableFooterView = self.NewCollectionView;
    self.NewCollectionView.bounces = NO;
    [self.NewCollectionView registerClass:[CYHDrawNewCell class] forCellWithReuseIdentifier:@"New"];
    
    // 下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    self.WinTableView.header = header;
    
    // 上拉刷新
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    self.WinTableView.footer = footer;
    
    self.URL = @"http://api.huapipi.com/painting/top/normal?days=3&deviceId=1826162&pnum=1&uid=1033268";
    self.isSelete = 1;
}


#pragma mark - createData
- (void)createData {
    if ([self.WinTableView.footer isRefreshing]) {
        
        self.pnum++;
        NSString *url = nil;
        
        if (self.isSelete) {
            url = [NSString stringWithFormat:@"http://api.huapipi.com/painting/top/normal?days=3&deviceId=1826162&pnum=%ld&uid=1033268&ver=%@", self.pnum, self.verStr];
        } else {
            url = [NSString stringWithFormat:@"http://api.huapipi.com/painting/top/newcomer?days=3&deviceId=1826162&pnum=%ld&uid=1033268&ver=%@", self.pnum, self.verStr];
        }
        [ToolNetWorking ToolNetWorking:url block:^(id result) {
            
            NSMutableArray *tempArr = [NSMutableArray array];
            tempArr = [HotModel BaseModel:result[@"rows"]];
            [self.fourArr addObjectsFromArray:tempArr];
            
            // 拼接网址刷新
            HotModel *tempModel = self.fourArr.lastObject;
            self.verStr = tempModel.ver;
            
            if (self.fourArr.count > 0 && self.WinArr.count > 0) {
                
                [self.NewCollectionView reloadData];
                [self.WinTableView.footer endRefreshing];
            }
            
        }];
        
    } else {
        
        self.pnum = 1;
        if (self.isSelete) {
            self.URL = @"http://api.huapipi.com/painting/top/normal?days=3&deviceId=1826162&pnum=1&uid=1033268";
        }else {
            self.URL = @"http://api.huapipi.com/painting/top/newcomer?days=3&deviceId=1826162&pnum=1&uid=1033268";
        }
        
        [ToolNetWorking ToolNetWorking:self.URL block:^(id result) {
            // winArr
            self.WinArr = [NSMutableArray array];
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSInteger i = 0; i < 3; i++) {
                [tempArr addObject:result[@"rows"][i]];
            }
            self.WinArr = [HotModel BaseModel:tempArr];
            
            // fourArr
            NSMutableArray *tempFourArr = [NSMutableArray array];
            for (NSInteger i = 3; i < [result[@"rows"] count]; i++) {
                [tempFourArr addObject:result[@"rows"][i]];
            }
            self.fourArr = [HotModel BaseModel:tempFourArr];
            
            // 拼接网址刷新
            HotModel *tempModel = self.fourArr.lastObject;
            self.verStr = tempModel.ver;
            
            if (self.WinArr.count > 0 && self.fourArr.count > 0) {
                [self.WinTableView reloadData];
                [self.NewCollectionView reloadData];
                [self.WinTableView.header endRefreshing];
            }
        }];
    }
}



#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Win"];
    
    HotModel *model = self.WinArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
//     根据文本字体大小来计算文本的cell高度
//     第一个参数: 文字显示的最大尺寸范围(375, 0)(0 代表最大的意思)
//     第二个参数:
    CGRect rect = [[self.WinArr[indexPath.row] content][@"word"] boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return VIEW_HEIGHT / 2 + 230 + rect.size.height;
    return 200;
}

#pragma mark - 标题
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 30)];
    headView.backgroundColor = [UIColor whiteColor];
    UIButton *Allbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    if (self.isSelete) {
        
        [Allbutton setTintColor:[UIColor redColor]];
    } else {
        [Allbutton setTintColor:[UIColor lightGrayColor]];
    }
    
    [Allbutton setTitle:@"总榜" forState:UIControlStateNormal];
    [headView addSubview:Allbutton];
    Allbutton.frame = CGRectMake(0, 0, VIEW_WIDTH / 2, 30);
    [Allbutton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    Allbutton.tag = 2001;
    
    UIButton *NewButton = [UIButton buttonWithType:UIButtonTypeSystem];
    if (self.isSelete) {
        
        [NewButton setTintColor:[UIColor lightGrayColor]];
    } else {
        [NewButton setTintColor:[UIColor redColor]];
    }
    
    [NewButton setTitle:@"新人榜" forState:UIControlStateNormal];
    [headView addSubview:NewButton];
    NewButton.frame = CGRectMake(VIEW_WIDTH / 2, 0, VIEW_WIDTH / 2, 30);
    [NewButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    NewButton.tag = 2000;
    
    return headView;
}

#pragma mark - 换网址
- (void)ButtonAction:(UIButton *)button {
    UIButton *AllButton = (UIButton *)[self.view viewWithTag:2001];
    UIButton *NewButton = (UIButton *)[self.view viewWithTag:2000];
    if (button == AllButton) {
        self.isSelete = 1;
        self.URL = @"http://api.huapipi.com/painting/top/normal?days=3&deviceId=1826162&pnum=1&uid=1033268";
        [self.WinTableView setContentOffset:CGPointMake(0, -64) animated:YES];
        [self createData];
    } else if (button == NewButton){
        self.isSelete = 0;
        self.URL = @"http://api.huapipi.com/painting/top/newcomer?days=3&deviceId=1826162&pnum=1&uid=1033268";
        [self.WinTableView setContentOffset:CGPointMake(0, -64) animated:YES];
        [self createData];
    }
}



#pragma mark - collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.fourArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHDrawNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"New" forIndexPath:indexPath];
        
    HotModel *model = self.fourArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHHotOtherVC *otherVC = [[CYHHotOtherVC alloc] init];
    HotModel *model = self.self.fourArr[indexPath.item];
    otherVC.Secid = model.secid;
    [self.navigationController pushViewController:otherVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
